<template>
    <view class="container">
        <u--text text="用户注册" size="20" bold color="#333" align="center" marginBottom="30"></u--text>

        <!-- 注册表单 -->
        <u--form :model="form" ref="registerForm">
            <u-form-item label="手机号" prop="phone">
                <u--input v-model="form.phone" placeholder="请输入手机号" clearable></u--input>
            </u-form-item>

            <u-form-item label="密码" prop="password">
                <u--input type="password" v-model="form.password" placeholder="请输入密码" clearable></u--input>
            </u-form-item>

            <u-form-item label="确认密码" prop="confirmPassword">
                <u--input type="password" v-model="form.confirmPassword" placeholder="请再次输入密码" clearable></u--input>
            </u-form-item>
        </u--form>

        <!-- 注册按钮 -->
        <u-button text="注册" type="primary" @click="handleRegister" style="margin-top: 20px;"></u-button>

        <!-- 登录跳转 -->
        <view class="link">
            <text @click="toLogin">已有账号？去登录</text>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                form: {
                    phone: '',
                    password: '',
                    confirmPassword: ''
                }
            }
        },
        methods: {
            handleRegister() {
                // 前端简单校验
                if (!this.form.phone || !/^1[3-9]\d{9}$/.test(this.form.phone)) {
                    uni.showToast({
                        title: '手机号格式错误',
                        icon: 'none'
                    });
                    return;
                }
                if (!this.form.password || this.form.password.length < 6) {
                    uni.showToast({
                        title: '密码至少6位',
                        icon: 'none'
                    });
                    return;
                }
                if (this.form.password !== this.form.confirmPassword) {
                    uni.showToast({
                        title: '两次密码不一致',
                        icon: 'none'
                    });
                    return;
                }

                // 发送注册请求
                uni.request({
                    url: 'http://localhost:8080/api/register',
                    method: 'POST',
                    data: {
                        phone: this.form.phone,
                        password: this.form.password
                    },
                    success: (res) => {
                        if (res.data.success) {
                            uni.showToast({
                                title: '注册成功',
                                icon: 'success'
                            });
                            // 注册成功后跳转到登录页
                            setTimeout(() => {
                                uni.redirectTo({
                                    url: '/pages/login/login'
                                });
                            }, 1000);
                        } else {
                            uni.showToast({
                                title: res.data.message || '注册失败',
                                icon: 'none'
                            });
                        }
                    },
                    fail: () => {
                        uni.showToast({
                            title: '网络错误',
                            icon: 'none'
                        });
                    }
                });
            },
            toLogin() {
                uni.redirectTo({
                    url: '/pages/login/login'
                });
            }
        }
    }
</script>

<style scoped>
    .container {
        padding: 40px 20px;
    }

    .link {
        text-align: center;
        margin-top: 20px;
    }

    .link text {
        color: #007AFF;
    }
</style>